package com.thinkgem.jeesite.modules.erp.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.erp.dao.DaoSupport;
import com.thinkgem.jeesite.modules.erp.dao.GoodsTypeDao;
import com.thinkgem.jeesite.modules.erp.entity.Compare;
import com.thinkgem.jeesite.modules.erp.entity.Goods;
import com.thinkgem.jeesite.modules.erp.entity.GoodsType;
import com.thinkgem.jeesite.modules.erp.entity.MaterialType;
import com.thinkgem.jeesite.modules.erp.entity.Order;
import com.thinkgem.jeesite.modules.erp.entity.OutAndInWarehouseOrder;
import com.thinkgem.jeesite.modules.erp.entity.Page;
import com.thinkgem.jeesite.modules.oa.dao.OaNotifyDao;
import com.thinkgem.jeesite.modules.oa.entity.OaNotify;

/**
 * 入库管理
 * @author lx
 *
 */
@Service
@Transactional
public class GoodsTypeService extends CrudService<OaNotifyDao, OaNotify>{
	
	@Autowired
	private GoodsTypeDao goodsTypeDao;
	
	@Resource(name = "daoSupport")
	private DaoSupport dao;

	//查询出树形
	public List<GoodsType> queryGoodsTree(Map<String,Object> map){
		List<GoodsType> list = goodsTypeDao.queryGoodsTree(map);
		for(GoodsType g:list){
			g.setId(g.getId()+"-"+g.getGrade());
			if(g.getList().size()>0){
				g.setState("closed");
			}
		}
		return list;
	}

	/**
	 * 
	 * @param typeId 类型标号-类型等级
	 * @return
	 */
	public GoodsType queryLinkGoods(String typeId) {
		String[] sid = typeId.split("-");
		Map<String,Object>map=new HashMap<String,Object>();
		map.put("fatherGrade", sid[1]);
		map.put("fatherCode", sid[0]);
		map.put("sonGrade", Integer.parseInt(sid[1])+1);
		//返回父节点下面的其他节点
		List<GoodsType> typelist=goodsTypeDao.queryLinkType(map);
		for(GoodsType type : typelist){
			type.setId(type.getId()+"-"+type.getGrade());
			type.setState("closed");
		}
		//根据编号返回父节点和父下面的物品
		GoodsType linklist=goodsTypeDao.queryLinkGoods(map);
		List<GoodsType> list = linklist.getList();
		typelist.addAll(list);
		linklist.setList(typelist);
		
		return linklist;
	}

	public List<GoodsType> queryLinkGoodsOt(String sonTypeId) {
		String[] sid = sonTypeId.split("-");
		Map<String,Object>map=new HashMap<String,Object>();
		map.put("fatherGrade", sid[1]);
		map.put("fatherCode", sid[0]);
		map.put("sonGrade", Integer.parseInt(sid[1])+1);
		List<GoodsType> typelist=goodsTypeDao.queryLinkType(map);
		for(GoodsType type : typelist){
			type.setId(type.getId()+"-"+type.getGrade());
			type.setState("closed");
		}
		List<GoodsType> linklist=goodsTypeDao.queryLinkGoodsOt(map);
		typelist.addAll(linklist);
		return typelist;
	}

	public String goodsStatistics(OutAndInWarehouseOrder order) throws JsonProcessingException {
		Map<String,Object>map=new HashMap<String,Object>();
		
		List<OutAndInWarehouseOrder>rows=goodsTypeDao.goodsStatistics(order);
		List<Map<String,Object>>listMap=new ArrayList<Map<String,Object>>();
		int in=0;//次数
		int out=0;
		Integer inAmount=0;//总量
		Integer outAmount=0;
		for(OutAndInWarehouseOrder or:rows){
			boolean type = or.isType();
			if(type){//入库
				//次数
				inAmount=inAmount+or.getAmount();
				//总数
				or.setInAmount(or.getAmount());
				in++;
			}else{
				outAmount=outAmount+or.getAmount();
				or.setOutAmount(or.getAmount());
				out++;
			}
		}
		Map<String,Object>one=new HashMap<String,Object>();
		Map<String,Object>two=new HashMap<String,Object>();
		one.put("executorCode", "平均数：");
		one.put("inAmount", in==0?0:(float)inAmount/in);
		one.put("outAmount", out==0?0:(float)outAmount/out);
		two.put("executorCode", "总数：");
		two.put("inAmount", inAmount);
		two.put("outAmount", outAmount);
		listMap.add(one);
		listMap.add(two);
		map.put("rows", rows);
		map.put("footer", listMap);
		ObjectMapper om = new ObjectMapper ();
		String depstr = om.writeValueAsString (map);
		if(null!=depstr&&!"".equals(depstr)){
			depstr=depstr.replaceAll("time", "execDate").replaceAll("executor", "executorCode").replaceAll("code", "oCode").replaceAll("executorCodeCode","executorCode");
		}
		return depstr;
	}

	public List<Goods> dimQueryLinkGoods(Map<String, Object> map) {
		List<Goods> list = goodsTypeDao.dimQueryLinkGoods(map);
		return list;
	}

	public List<Goods> dimQueryLinkGoodsByNames(List<String> strings) {
		List<Goods> list = goodsTypeDao.dimQueryLinkGoodsByNames(strings);
		return list;
	}

	public List<Goods> queryResidue(Map<String, Object> map) {
		List<Goods> list = goodsTypeDao.queryResidue(map);
		return list;
	}

	public Page<Goods> ajQueryResidue(Map<String, Object> map) {
		Page<Goods> page=new Page<Goods>();
		//数量
		Integer total=goodsTypeDao.querySum(map);
		//对应的数
		List<Goods> list=goodsTypeDao.ajQueryResidue(map);
		page.setTotal(total);
		page.setRows(list);
		return page;
	}

	public Page<Goods> ajQueryResidueParameter(Map<String, Object> map) {
		Page<Goods> page=new Page<Goods>();
		//数量
		Integer total=goodsTypeDao.querySum2(map);
		//对应的数
		List<Goods> list=goodsTypeDao.ajQueryResidue2(map);
		page.setTotal(total);
		page.setRows(list);
		return page;
	}

	public void deleteGoods(String goodsCode) {
		goodsTypeDao.deleteGoods(goodsCode);
	}

	public String saveGoods(Goods goods) {
		String id=goodsTypeDao.isSaveGoodsRepeat(goods);//校验物料编号或名称是否重复
		if(null!=id){
			return "0";
		}else{
			goodsTypeDao.saveGoods(goods);
			return "1";
		}
	}

	public String updateGoods(Goods goods) {
		String id=goodsTypeDao.isSaveGoodsRepeat(goods);
		if(null!=id){
			return "0";
		}else{
			goodsTypeDao.updateGoods(goods);
			return "1";
		}
	}

	public String addMaterial(MaterialType type) {
		String id=goodsTypeDao.isSaveMaterialRepeat(type);
		if(null!=id){
			return "0";
		}else{
			goodsTypeDao.addMaterial(type);
			return "1";
		}
	}

	public String updateMaterial(MaterialType type) {
		String id=goodsTypeDao.isSaveMaterialRepeat(type);
		if(null!=id){
			return "0";
		}else{
			goodsTypeDao.updateMaterial(type);
			return "1";
		}
	}

	public List<GoodsType> parentQueryGoodsTree(Map<String, Object> map) throws Exception {
		@SuppressWarnings("unchecked")
		List<GoodsType> list = (List<GoodsType>) dao.findForList("ParentErpMapper.parentQueryGoodsTree", map);
		for(GoodsType g:list){
			g.setId(g.getId()+"-"+g.getGrade());
			if(g.getList().size()>0){
				g.setState("closed");
			}
		}
		return list;
	}

	public List<Goods> queryParentResidue(Map<String, Object> map) throws Exception {
		@SuppressWarnings("unchecked")
		List<Goods> list = (List<Goods>) dao.findForList("ParentErpMapper.queryParentResidue", map);
		return list;
	}

	public Page<Goods> ajQueryParentResidue(Map<String, Object> map) throws Exception {
		Page<Goods> page=new Page<Goods>();
		//数量
		Integer total=(Integer) dao.findOne("ParentErpMapper.querySum", map);
		//对应的数
		@SuppressWarnings("unchecked")
		List<Goods> list=(List<Goods>) dao.findForList("ParentErpMapper.ajQueryParentResidue", map);
		page.setTotal(total);
		page.setRows(list);
		return page;
	}

	public List<Goods> dimQueryParentLinkGoods(Map<String, Object> map) throws Exception {
		@SuppressWarnings("unchecked")
		List<Goods>list=(List<Goods>) dao.findForList("ParentErpMapper.dimQueryParentLinkGoods", map);
		return list;
	}

	public List<Map<String,Object>> queryAllClass() throws Exception {
		@SuppressWarnings("unchecked")
		List<Map<String,Object>> list=(List<Map<String, Object>>) dao.findAll("ParentErpMapper.queryAllClass");
		return list;
	}

	public void updateClass(List<Map<String, Object>> list) {
		goodsTypeDao.deleteAllClass();
		goodsTypeDao.updateClass(list);
	}
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> queryInsert() throws Exception {
		List<String> local=goodsTypeDao.queryAllInventory();//获取本地的所有物料编号
		List<String> remote=(List<String>) dao.findAll("ParentErpMapper.queryAllInventory");//获取远程的所的编号
		System.out.println("本地条数："+local.size()+"    远程条数："+remote.size());
		
		remote.removeAll(local);//远程-本地=新增
		String join = StringUtils.join(remote.toArray(), "','");  
		//查询具体新增的内容
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("join", "'"+join+"'");
		List<Map<String,Object>> resultList=(List<Map<String, Object>>) dao.findForList("ParentErpMapper.queryInventorysInfo", map);
		return resultList;
	}
	public List<String> queryDelete() throws Exception {
		List<String> local=goodsTypeDao.queryAllInventory();//获取本地的所有物料编号
		@SuppressWarnings("unchecked")
		List<String> remote=(List<String>) dao.findAll("ParentErpMapper.queryAllInventory");//获取远程的所的编号
		local.removeAll(remote);//本地-远程=需要删除的
		return local;
	}
	@SuppressWarnings("unchecked")
	public List<Compare> queryUpdate() throws Exception {
		List<Compare> local=goodsTypeDao.queryAllInventoryCompare();
		List<Compare> remote=(List<Compare>) dao.findAll("ParentErpMapper.queryAllInventoryCompare");
		List<Compare> subtract = ListUtils.subtract(remote, local);
		return subtract;
	}

	public void updateInventory(List<Map<String, Object>> insertList, List<String> deleteList,
			List<Compare> updateList) {
		//新增
		if(insertList.size()>0&&insertList!=null)
		goodsTypeDao.insertInventory(insertList);
		//修改
		if(updateList.size()>0&&updateList!=null)
		goodsTypeDao.upateInventory(updateList);
		//最后删除
		String join = StringUtils.join(deleteList.toArray(), "','");
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("join", "'"+join+"'");
		if(deleteList.size()>0&&deleteList!=null)
		goodsTypeDao.deleteInventory(map);
	}

	public void updateInventory2(List<Map<String, Object>> insertList) {
		//新增
		goodsTypeDao.insertInventory(insertList);
		
	}

	public Page<OutAndInWarehouseOrder> queryOrdersInfo(Order order) {
		Page<OutAndInWarehouseOrder> page=new Page<OutAndInWarehouseOrder>();
		List<OutAndInWarehouseOrder> amount=goodsTypeDao.queryAllOrders(order);
		List<OutAndInWarehouseOrder> list=goodsTypeDao.queryOrdersInfo(order);
		
		page.setTotal(amount.size());
		page.setRows(list);
		return page;
	}

}
